<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

//define field to generate form
if (!function_exists('usersFieldForm')) {

    function usersFieldForm($user = array(), $role = array(), $statusOpt = array())
    {
        global $CFG;
        $CI = & get_instance();
        if (count($user) && !empty($user)) {
            $username = $user['username'];
            $first_name = $user['first_name'];
            $last_name = $user['last_name'];
            $email = $user['email'];
            $role_id = $user['role_id'];
            $status = $user['status'];
            $avatar = $user['avatar'];
            if ($avatar)
                $thumbAvatar = getThumbImage($CFG->item('folder_avatar') . $avatar, 50, 50);
        }

        $_field_form_users = array(
            'username' => array('name' => 'username', 'id' => 'frm-username',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_username'),
                'value' => set_value('username', isset($username) ? $username : '')),
            'password' => array('name' => 'password', 'id' => 'frm-password',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_pwd'), 'autocomplete' => 'off',
                'value' => set_value('password')),
            'password_confirm' => array('name' => 'password_confirm', 'id' => 'frm-password-confirm',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_pwd_confirm'), 'autocomplete' => 'off',
                'value' => set_value('password_confirm')),
            'first_name' => array('name' => 'first_name', 'id' => 'frm-first-name',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_firstname'),
                'value' => set_value('first_name', isset($first_name) ? $first_name : '')),
            'last_name' => array('name' => 'last_name', 'id' => 'frm-last-name',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_lastname'),
                'value' => set_value('last_name', isset($last_name) ? $last_name : '')),
            'email' => array(
                'name' => 'email',
                'id' => 'frm-email',
                'class' => 'span3',
                'placeholder' => $CI->lang->line('Pls_enter_email'),
                'value' => set_value('email', isset($email) ? $email:'')
            ),
            'current_avatar' => array('value' => isset($thumbAvatar) ? $thumbAvatar : '',
                'width' => '50', 'height' => '50'
            ),
            'avatar' => array('name' => 'avatar', 'id' => 'frm-avatar',
            ),
            'role_id' => array(
                'name' => 'role_id',
                'options' => $role,
                'selected' => isset($role_id) ? array($role_id) : array(),
            ),
            'status' => array(
                'name' => 'status',
                'options' => $CFG->item('status'),
                'selected' => isset($status) ? array($status) : array(),
            ),
            'submit' => array('class' => 'btn btn-primary', 'data-loading-text' => $CI->lang->line('Loading'),
                'id' => 'form-login-btnLogin', 'value' => $CI->lang->line('Save')),
        );

        return $_field_form_users;
    }

}

if (!function_exists('usersFieldSearch')) {

    function usersFieldSearch($role = array(), $usrSearchData = array())
    {
        $CI = & get_instance();
        global $CFG;
        $statusText = array('' => $CI->lang->line('Pls_select_role'));
        $statusArr = $CFG->item('status');
        $statusArr = array_merge($statusText, $statusArr);
        $role_id = isset($usrSearchData['role_id']) ? $usrSearchData['role_id'] : '';
        $status = isset($usrSearchData['status']) ? $usrSearchData['status'] : '';
        $usrSearch = isset($usrSearchData['textsearch']) ? $usrSearchData['textsearch'] : '';
        $loading = $CI->lang->line('Loading');
        $_field_search_users = array(
            'textsearch' => array('name' => 'textsearch', 'id' => 'frm-textsearch',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_keyword'),
                'value' => set_value('textsearch', $usrSearch)
            ),
            'submit' => array('name' => 'btn-Search', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnSearch', 'value' => $CI->lang->line('Search')),
            'reset' => array('name' => 'btn-Reset', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnReset', 'value' => $CI->lang->line('Reset')),
        );

        return $_field_search_users;
    }

}

if (!function_exists('usersFieldAction')) {

    function usersFieldAction()
    {
        $CI = & get_instance();
        global $CFG;
        $loading = $CI->lang->line('Loading');
        $_field_action = array(
            'event' => array(
                'name' => 'event',
                'options' => $CFG->item('event'),
                'selected' => isset($event) ? array($event) : array(),
            ),
            'submit' => array('name' => 'btn-Update', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnEvent', 'value' => $CI->lang->line('Update')),
        );

        return $_field_action;
    }

}

if (!function_exists('usersFieldPwd')) {

    function usersFieldPwd()
    {
        $CI = & get_instance();
        $loading = $CI->lang->line('Loading');
        $_field_changepwd = array(
            'newpwd' => array(
                'name' => 'newpwd', 'id' => 'frm-newpwd',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_newpwd'),
            ),
            'newpwdconfirm' => array(
                'name' => 'newpwdconfirm', 'id' => 'frm-newpwdconfirm',
                'class' => 'span3', 'placeholder' => $CI->lang->line('Pls_enter_newpwd_confirm'),
            ),
            'submit' => array('name' => 'btn-Update', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnEvent', 'value' => $CI->lang->line('Update')),
        );

        return $_field_changepwd;
    }

}

if (!function_exists('usersFieldPwdValidate')) {

    function usersFieldPwdValidate()
    {
        $CI = & get_instance();
        $_field_validate_changepwd = array(
            array(
                'field' => 'newpwd',
                'label' => $CI->lang->line('New_pwd'),
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'newpwdconfirm',
                'label' => $CI->lang->line('New_pwd_confirm'),
                'rules' => 'trim|required|matches[newpwd]',
            ),
        );
        return $_field_validate_changepwd;
    }

}

//define field to validate form
if (!function_exists('usersFieldValidate')) {

    function usersFieldValidate()
    {
        $CI = & get_instance();
        $_field_validate_users = array(
            array(
                'field' => 'username',
                'label' => $CI->lang->line('Username'),
                'rules' => 'trim|required|max_length[15]|xss_clean'
            ),
            array(
                'field' => 'password',
                'label' => $CI->lang->line('Password'),
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'password_confirm',
                'label' => $CI->lang->line('PasswordConfirm'),
                'rules' => 'trim|required|matches[password]',
            ),
            array(
                'field' => 'email',
                'label' => $CI->lang->line('Email'),
                'rules' => 'trim|required',
            ),
            array(
                'field' => 'role_id',
                'label' => $CI->lang->line('Role'),
                'rules' => 'required',
            )
        );
        return $_field_validate_users;
    }

}

if (!function_exists('userUploadCSVForm')) {

    function userUploadCSVForm()
    {
        $CI = & get_instance();
        $loading = $CI->lang->line('Loading');
        $_field_upload = array(
            'csv_file' => array('name' => 'csv_file', 'id' => 'frm-csv-file',
            ),
            'submit' => array('name' => 'btn-Update', 'class' => 'btn btn-primary', 'data-loading-text' => $loading,
                'id' => 'form-btnEvent', 'value' => $CI->lang->line('Import')),
        );
        return $_field_upload;
    }

}
